package com.mes.utils;

import com.yy.base.common.dialect.MapResult;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class DBConctionQuery {
	private Connection conn = null;
	public DBConctionQuery() {
		try {
			conn = ConnectionUtil.getConnection();
		} catch (Exception e) {
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> query(String sql, Object... objs) throws SQLException {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			if (objs != null && objs.length > 0) {
				for (int i = 1;i <= objs.length;i ++) {
					if (objs[i - 1] instanceof java.util.Date) {
						ps.setTimestamp(i, new Timestamp(((Date)objs[i - 1]).getTime()));
					} else {
						ps.setObject(i, objs[i - 1]);
					}
				}
			}
			rs = ps.executeQuery();
			ResultSetMetaData rsmd = ps.getMetaData();
			int size = rsmd.getColumnCount();
			String[] names = new String[size];
			for (int i = 1; i <= size; i++) {
				try {
					names[i - 1] = rsmd.getColumnName(i);
				} catch (Exception e) {
				}
			}
			
			Map<String, Object> map = null;
			while (rs.next()) {
				map = new MapResult();
				for (int i = 0; i < names.length; i++) {
					map.put(names[i].toUpperCase(), rs.getObject(names[i]));
				}
				list.add(map);
			}
		} finally {
			try {
				rs.close();
			} catch (Exception e) {
			}
			try {
				ps.close();
			} catch (Exception e) {
			}
		}
		return list;
	}

	public Map<String, Object> queryUnique(String sql, Object... objs) throws SQLException {
		List<Map<String, Object>> list = query(sql, objs);
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}
	
	public int execute(String sql, Object... objs) throws SQLException {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			if (objs != null && objs.length > 0) {
				for (int i = 1;i <= objs.length;i ++) {
					if (objs[i - 1] instanceof Date) {
						ps.setTimestamp(i, new Timestamp(((Date)objs[i - 1]).getTime()));
					} else {
						ps.setObject(i, objs[i - 1]);
					}
				}
			}
			return ps.executeUpdate();
		} finally {
			try {
				rs.close();
			} catch (Exception e) {
			}
			try {
				ps.close();
			} catch (Exception e) {
			}
		}
	}
	
	public String callBack(String sql, Object... objs) throws SQLException {
		CallableStatement proc = null;
		try {
			proc = conn.prepareCall(sql);
			int i = 1;
			if (objs != null && objs.length > 0) {
				for (;i <= objs.length;i ++) {
					if (objs[i - 1] instanceof Date) {
						proc.setTimestamp(i, new Timestamp(((Date)objs[i - 1]).getTime()));
					} else {
						proc.setObject(i, objs[i - 1]);
					}
				}
			}
			proc.registerOutParameter(i, Types.VARCHAR);
			proc.execute();
			
			return proc.getString(i);
		} finally {
			try {
				proc.close();
			} catch (Exception e) {
			}
		}
	}
	
	public void beginTransaction() {
		try {
			conn.setAutoCommit(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void commit() {
		if (conn != null) {
			try {
				conn.commit();
			} catch (Exception e) {
			}
		}
	}
	
	public void rollback() {
		if (conn != null) {
			try {
				conn.rollback();
			} catch (Exception e) {
			}
		}
	}
	
	public void close() {
		if (conn != null) {
			try {
				ConnectionUtil.resetConnection(conn);
			} catch (Exception e) {
			}
		}
	}
}